package framework.system.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import framework.core.dao.IBaseDao;
import framework.core.util.Kit;
import framework.core.util.ListRange;
import framework.system.entity.SysRole;
import framework.system.entity.front.Role;
import framework.system.service.IRoleService;

/**
 * @ClassName: RoleService 
 * @Description: 角色Service实现
 * @author: Ching Wang
 * @date 2013-5-18 下午2:42:46 
 *
 */
@Service
public class RoleService implements IRoleService {

	@Resource private IBaseDao<SysRole> roleDao;
	
	/*
	 * (非 Javadoc) 
	 * <p>Title: findAll</p> 
	 * <p>Description: </p> 
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#findAll()
	 */
	@Override
	public List<SysRole> findAll() throws Exception {
		return roleDao.findAll(SysRole.class);
	}
	
	/*
	 * (非 Javadoc) 
	 * <p>Title: getRole</p> 
	 * <p>Description: </p> 
	 * @param roleId
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#getRole(java.lang.String)
	 */
	@Override
	public SysRole getRole(String roleId) throws Exception {
		return roleDao.findById(SysRole.class, roleId);
	}
	
	/* 
	 * (非 Javadoc) 
	 * <p>Title: getCount</p> 
	 * <p>Description: </p> 
	 * @param criteria
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#getCount(org.hibernate.criterion.DetachedCriteria)
	 */
	@Override
	public long getCount(DetachedCriteria criteria) throws Exception {
		return roleDao.getCount(criteria);
	}
	
	/*
	 * (非 Javadoc) 
	 * <p>Title: listRole</p> 
	 * <p>Description: </p> 
	 * @param criteria
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#listRole(org.hibernate.criterion.DetachedCriteria, java.lang.String, java.lang.String)
	 */
	@Override
	public List<SysRole> listRole(DetachedCriteria criteria, String page, String rows) throws Exception {
		int limit = Kit.isNotNull(rows) ? Integer.parseInt(rows) : 0;
		int start = Kit.isNotNull(page) ? ((Integer.parseInt(page)-1) * limit) : 0;
		return roleDao.findByCriteria(criteria, start, limit);
	}
	
	/*
	 * (非 Javadoc) 
	 * <p>Title: saveOrUpdate</p> 
	 * <p>Description: </p> 
	 * @param sysRole
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#saveOrUpdate(framework.system.entity.SysRole)
	 */
	@Override
	public void saveOrUpdate(SysRole sysRole) throws Exception {
		if(Kit.isNotNull(sysRole.getId())){
			sysRole.setModifyDate(new Date());
			roleDao.update(sysRole);
		}
		else{
			sysRole.setCreateDate(new Date());
			roleDao.save(sysRole);
		}
	}
	
	/*
	 * (非 Javadoc) 
	 * <p>Title: delRole</p> 
	 * <p>Description: </p> 
	 * @param roleIds
	 * @return
	 * @throws Exception 
	 * @see framework.system.service.IRoleService#delRole(java.lang.String[])
	 */
	@Override
	public ListRange<Role> delRole(String[] roleIds) throws Exception {
		ListRange<Role> range = new ListRange<Role>();
		boolean flag = true;
		for(String rid : roleIds){
			SysRole sysRole = roleDao.findById(SysRole.class, rid);
			if(sysRole != null){
				if(sysRole.getSysUsers() != null && sysRole.getSysUsers().size() > 0){
					flag = false;
					range.setMessage("角色<font style='font-weight:bold;color:#ff0000;'>"+sysRole.getName()+"</font>正在使用中，请确认.");
					break;
				}
				else{
					flag &= roleDao.delete(sysRole);
					if(!flag){
						range.setMessage("删除角色信息失败，请确认.");
						break;
					}
				}
			}
		}
		range.setSuccess(flag);
		return range;
	}
}
